package com.wangyadong.hobby.common.utils;

import com.wangyadong.hobby.common.constants.RedisKeyConstants;
import redis.clients.jedis.Jedis;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.stream.Collectors;

public class MapObjectConvert {


    public static Map<String, String> objectToMap(Object object) throws IllegalAccessException {
        Field[] declaredFields = object.getClass().getDeclaredFields();
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < declaredFields.length; i++) {
            Field declaredField = declaredFields[i];
            if (declaredField.get(object) != null) {
                declaredField.setAccessible(true);
                map.put(declaredFields[i].getName(), declaredField.get(object).toString());
            }
        }
        return map;
    }

    public static Object maptoObject(Map<String, String> map, Class<?> beanClass) throws IllegalAccessException, InstantiationException {
        Object object = beanClass.newInstance();
        Field[] declaredFields = object.getClass().getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field declaredField = declaredFields[i];
            int modifiers = declaredField.getModifiers();
            if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) {
                continue;
            }
            declaredField.setAccessible(true);
            declaredField.set(declaredField, map.get(declaredField.getName()));
        }
        return object;
    }


}
